﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBWTProject.models;
using DBWTProject.persistence;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Globalization;
using DBWTProject.route.util;

namespace DBWTProject.route
{
    public partial class RouteDetail : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            String gVisApi = @"<script type=""text/javascript"" src=""http://www.google.com/jsapi""></script><script type=""text/javascript"">google.load('visualization', '1', {packages: ['corechart']});</script>";

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "gVisApi", gVisApi); 
            
            User user = (User)Session[Constants.SESSIONVAR_CURRENTUSER];

            if (user == null)
            {//nicht eingeloggt
                Response.Redirect("/");
                return;
            }

            if (!Page.IsPostBack)//Nur bei GET
            {
                CultureInfo cultureInfoDE = new CultureInfo("de-DE", false);
                
                String routeId = Request.QueryString["id"];
                Route route = RouteDAO.GetRoute(Convert.ToInt32(routeId));

                totalDistanceLabel.Text = route.Distance.ToString("0.0", cultureInfoDE);
                elevationCoveredLabel.Text = route.Elevation.ToString("0.00", cultureInfoDE);
                totalDurationLabel.Text = (route.Duration == TimeSpan.Zero) ? "n. def." : route.Duration.ToString();
                meanSpeedLabel.Text = route.MeanSpeed.ToString("0.0", cultureInfoDE);

                /*                IList<KeyValuePair<string, string>> distanceHeightProfile = ChartUtil.getCoorForHeightProfile(route);
                foreach (KeyValuePair<string, string> coor in distanceHeightProfile)
                {
                    elevationProfile.Add("[" + coor.Key + ", " + coor.Value + "]");
                        
                }*/

                CultureInfo cultureInfoUS = new CultureInfo("en-US", false);

                List<Tuple<double, double>> elevationProfilePoints;
                List<Tuple<double, double>> speedProfilePoints;
                ChartUtil.CalculateProfiles(route, out elevationProfilePoints, out speedProfilePoints);

                List<string> elevationProfile = new List<string>();
                foreach (Tuple<double, double> elevationItem in elevationProfilePoints)
                {
                    elevationProfile.Add("[" + elevationItem.Item1.ToString("0.0", cultureInfoUS) + ", " + elevationItem.Item2.ToString("0.0", cultureInfoUS) + "]");   
                }

                List<string> speedProfile = new List<string>();
                foreach (Tuple<double, double> speedProfileItem in speedProfilePoints)
                {
                    speedProfile.Add("[" + speedProfileItem.Item1.ToString("0.0", cultureInfoUS) + ", " + speedProfileItem.Item2.ToString("0.0", cultureInfoUS) + "]");
                }

                //speedProfile.Add("[" + runningDistance.ToString("0.0", cultureInfoUS) + ", " + (distanceDiff / timeDiff.TotalHours).ToString("0.0", cultureInfoUS) + "]");
                //elevationProfile.Add("[" + runningDistance.ToString("0.0", cultureInfoUS) + ", " + coordinateList[x].Item3.ToString("0.00", cultureInfoUS) + "]");

                String elevationProfileJS = @"<script type=""text/javascript"">
                    function drawVisualization() {
                        // Create and populate the data table.
                        var elevationProfile = google.visualization.arrayToDataTable([['Distance', 'Elevation'], " + String.Join(",", elevationProfile.ToArray()) + @"]);
                        var speedProfile = google.visualization.arrayToDataTable([['Distance', 'Speed'], " + String.Join(",", speedProfile.ToArray()) + @"]);

                        var config1 = {
                            curveType: 'function',
                            legend : {position: 'none'},
                            width: 750,
                            height: 400,
                            hAxis: {title: 'Distance (km)'},
                            vAxis: {title: 'Elevation (m)'},
                        };

                        var config2 = {
                            curveType: 'function',
                            legend : {position: 'none'},
                            width: 750,
                            height: 400,
                            hAxis: {title: 'Distance (km)'},
                            vAxis: {title: 'Speed (km/h)', minValue: 0},
                        };

                        // Create and draw the visualization.
                        new google.visualization.LineChart(document.getElementById('" + elevationProfPanel.ClientID + @"')).
                            draw(elevationProfile, config1);

                        new google.visualization.LineChart(document.getElementById('" + speedProfPanel.ClientID + @"')).
                            draw(speedProfile, config2);
                    }

                    google.setOnLoadCallback(drawVisualization);
                </script>";

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "elevationProfileJS", elevationProfileJS);
            }
        }
    }
}